<?php
/*IMPORTANT: 注意检查文件字符集是否为UTF-8*/
/**
 *
 * @当前socket支持：HTTP GET, HTTP POST
 * @作用：模拟HTTP访问操作访问特定资源
 *
 */
class sockstuff
{
	private $sock;
	private $host;
	private $port;
	private $path;
	private $method;
	private $protocol;
	private $cookie;
	/**
	 * @name __construct
	 * @author Jiong
	 * @description do nothing
	 * @parameters void
	 * @return void
	 * @date 2011-05-15
	 */
	public function __construct()
	{
		;
	}
	/**
	 * @name __destruct
	 * @author Jiong
	 * @parameters void
	 * @return void
	 * @date 2011-05-15
	 */
	public function __destruct()
	{
		if($this->sock)
		{
			fclose($this->sock);
		}
		$this->sock = null;
		$this->host = null;
		$this->method = null;
		unset($this->sock);
		unset($this->host);
		unset($this->method);
	}
	/**
	 * @name setHttpSock
	 * @author Jiong
	 * @parameters String host, int port, string method, string path. method must be "GET" or "POST".
	 * @return void
	 * @date 2011-05-15
	 */
	public function setHttpSock($host,$port,$method,$path,$cookie=null,$protocol="http")
	{
		$this->host=$host;
		$this->port=(int)$port;
		$this->path=$path;
		$method=strtoupper($method);
		if($method=="GET"||$method=="POST")
		{
			$this->method=$method;
		}
		else
		{
			$this->method="GET";
		}
		if (@$cookie)
		{
			$this->cookie = $cookie;
		}
		if ($protocol=="https")
		{
			$this->protocol = "ssl";
		}
		else
		{
			$this->protocol = "http";
		}
	}
	/**
	 * @name __setHttpSock
	 * @author Jiong
	 * @parameters String URL encoded param.
	 * @return String (Response from remote server)
	 * @date 2011-05-15
	 */
	public function httpSock($param)
	{
		include ('./config/socket.conf.php');
		$this->sock = fsockopen(($this->protocol=="ssl"?"ssl://":"").$this->host, $this->port, $errno, $errstr, $socketConfig['timeout']);
		if (!$this->sock)
		{	
			die("Socket Error");
		}
		if($this->method=="GET")
		{
			
			$header  = "GET {$this->path}?{$param} {$socketConfig['httpver']} \r\n";
			$header .= "Accept: */* \r\n";
			$header .= "Accept-Language: zh-cn,en-US;q=0.5\r\n";
			$header .= "User-Agent: {$socketConfig['useragent']}\r\n";
			$header .= "Host: {$this->host} \r\n";
			$header .= "Connection: Close\r\n";
			if(@$this->cookie)
			{
				$header .= "Cookie: {$this->cookie}\r\n";
			}
			$header .= "\r\n";
			//$header  = "GET /index.php HTTP/1.1\r\n";
			//$header .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
			//$header .= "Accept-Language: zh-cn,en-us;q=0.7,en;q=0.3\r\n";
			//$header .= "Connection: Close\r\n";
			//$header .= "Host: zabbix.service.chinanetcloud.com\r\n";
			//$header .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1\r\n";
			//$header .= "\r\n";
		}
		else if($this->method=="POST")
		{
			$header  = "POST {$this->path} {$socketConfig['httpver']} \r\n";
			$header .= "Accept: */* \r\n";
			$header .= "Accept-Language: zh-cn,en-US;q=0.5\r\n";
			$header .= "User-Agent: {$socketConfig['useragent']}\r\n";
			$header .= "Host: {$this->host} \r\n";
			$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
			$header .= "Content-Length: ".strlen($param)."\r\n";
			$header .= "Connection: close\r\n";
			if(@$this->cookie)
			{
				$header .= "Cookie: {$this->cookie}\r\n";
			}
			$header .= "\r\n";
			$header .= $param."\r\n";
		}
		else
		{
			die("Socket Error");
		}
		//echo $header;
		fwrite($this->sock, $header);
		$str = "";
		while (!feof($this->sock))
		{
			$str .= fread($this->sock, 1024);
		}
		return $str;
	}
}
?>